/**
 * 长时间的信息提示
 */
export function longTimeMessage(self, res, time) {
  let msg = '';

  if(typeof(res.msg) == 'object') {
      for (let i = 0; i <= res.msg.length - 1; i++) {
          msg += `<p style="line-height:20px;color:#f56c6c">${res.msg[i]}</p>`;
      }
  } else {
      msg = res.msg;
  }

  self.messageObj = self.$message({
      duration: time,
      type: 'warning',
      dangerouslyUseHTMLString: true,
      message: msg
  });
}

/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

export function formatDate(date) {
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();

  // 如果月份或日期小于10，则在前面补0
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = '0' + day;
  }
  if (hours < 10) {
    hours = '0' + hours;
  }
  if (minutes < 10) {
    minutes = '0' + minutes;
  }
  if (seconds < 10) {
    seconds = '0' + seconds;
  }

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}